<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>go 常用命令 | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>go 常用命令</h1>
<p>[TOC]</p>
<h3 id="go-run">go run</h3>
<p>编译并运行Go源码文件，编译的可执行文件在tmp目录下，这会影响相对路径</p>
<h3 id="go-build">go build</h3>
<p>编译源码文件，代码包，依赖包</p>
<h3 id="go-get--go-mod-download-xxx">go get / go mod download xxx</h3>
<p>动态获取远程代码包</p>
<p>go get 简介（1)
用于从远程代码仓库（如著名Github )上下载并安装代码包</p>
<p>受支持的代码版本控制系统有：Git、Mercurial ( hg )、SVN、Bazaar</p>
<p>指定的代码包会被下载到$G〇PATH中包含的第一个工作区的src目录中</p>
<pre tabindex="0"><code>常用命令参数
-d 只下载 不安装
-x 查看执行过程
-u 更新已下载的代码包
-fix 将旧版本的代码包转换成新版规则
</code></pre><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">mac</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">name</span>:<span style="color:#960050;background-color:#1e0010">~</span><span style="color:#f92672">/</span><span style="color:#66d9ef">go</span><span style="color:#f92672">/</span><span style="color:#a6e22e">golib</span><span style="color:#f92672">/</span><span style="color:#a6e22e">src</span><span style="color:#f92672">/</span><span style="color:#a6e22e">pkgtool</span><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#66d9ef">go</span> <span style="color:#a6e22e">install</span> <span style="color:#f92672">-</span><span style="color:#a6e22e">v</span> <span style="color:#f92672">-</span><span style="color:#a6e22e">work</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">在源码目录下</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">执行</span> <span style="color:#66d9ef">go</span> <span style="color:#a6e22e">install</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">会编译</span> <span style="color:#a6e22e">a</span> <span style="color:#a6e22e">文件到</span> 
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">HOME</span><span style="color:#f92672">/</span><span style="color:#a6e22e">golang</span><span style="color:#f92672">/</span><span style="color:#a6e22e">goc2p</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">bin</span><span style="color:#f92672">/</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">pkg</span><span style="color:#f92672">/</span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">linux_386</span><span style="color:#f92672">/</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">pkgtool</span>.<span style="color:#a6e22e">a</span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">src</span><span style="color:#f92672">/</span>
</span></span></code></pre></div><p>例：go get github.com/hyper-carrot/go_lib/logging</p>
<h3 id="go-install">go install</h3>
<p>go install简介( 1 )</p>
<p>​	用于编译并安装代码包或源码文件</p>
<p>​	安装代码包会在当前工作区的pkg/&lt;平台相关目录&gt;下生成归档文件</p>
<p>​	安装命令源码文件会在当前工作区的bin目录或$GOBIN目录下生成可执行文件</p>
<p>go install 简介(2 )</p>
<p>​	执行该命令且不追加任何参数时，它会试图把当前目录作为代码包并安装</p>
<p>​	执行该命令且以代码包的导入路径作为参数时,该代码包及其依赖会被安装</p>
<p>​	执行该命令且以命令源码文件及相关库源码文件作为参数时，只有这些文件会被编译并安装</p>
<p>在源文件目录下，执行<code>go install</code> ,编译源码，可执行文件放在 $GOBIN 目录下。文件以当前目录的 name 为 name。例：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">cd</span> <span style="color:#a6e22e">webserver</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">go</span> <span style="color:#a6e22e">install</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">生成</span> <span style="color:#a6e22e">bin</span><span style="color:#f92672">/</span><span style="color:#a6e22e">webserver</span>
</span></span></code></pre></div><h4 id="交叉编译">交叉编译</h4>
<p>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go</p>
<p>没有使用 cgo 库，操作系统是Linux，CPU架构为amd64</p>

    </article>
  </div>

  
  
  

  
</body>

</html>